package com.hexinfo.dmpro.server;

import com.hexinfo.dmpro.common.utils.SpringContextHolder;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package com.hexinfo.dmpro
 * @ClassName DmproAppRun
 * @Description
 * @Date 2022/10/7 12:18
 * @Version v1.0
 **/
@EnableAsync
@RestController
/**
 * 通过@EnableCaching注解自动化配置合适的缓存管理器（CacheManager），Spring Boot根据下面的顺序去侦测缓存提供者：
 Generic
 JCache (JSR-107)
 EhCache 2.x
 Hazelcast
 Infinispan
 Redis
 Guava
 Simple
 可以通过配置属性spring.cache.type来强制指定
 */
@Slf4j
@EnableCaching
@SpringBootApplication(scanBasePackages = {"com.hexinfo.dmpro"})
@EnableTransactionManagement
@Api(tags = "系统：系统启动验证")
@MapperScan({"com.hexinfo.dmpro.**.dao"})
public class DmproAppRun {

    @Value("${application.version}")
    private String appVersion;

    public static void main(String[] args) {
        SpringApplication.run(DmproAppRun.class, args);
    }

    @Bean
    public SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }

    @Bean
    public ServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory fa = new TomcatServletWebServerFactory();
        fa.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars", "[]{}"));
        return fa;
    }

    @GetMapping("/")
    @AnonymousAccess
    public String index(HttpServletResponse response) throws IOException {
        return "Backend service started successfully";
    }

    @ApiOperation("获取平台信息")
    @GetMapping(value = "/info")
    public ResponseEntity<Object> getPlatformInfo() {
        Map<String, Object> platformInfo = new HashMap<>(2);
        platformInfo.put("name", "Hexinfo Dmpro");
        platformInfo.put("version", appVersion);
        return ResponseEntity.ok(platformInfo);
    }

    /**
     * 将restTemplate注入到spring ioc容器
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
